const Koa = require('koa');
const path = require('path');
const static = require('koa-static');
const koaBody = require('koa-body');
const api = require('./api');
const compress = require('koa-compress');
const staticCache = require('koa-static-cache')
const redirectHtml = require('./middleware/redirectHtml');
const redirectProject = require('./middleware/redirectProject');
const args = require('minimist')(process.argv.slice(2));
const log = require('tools/log');
const uploadDir = path.join(__dirname, '../../files-temporary');
const maxFileSize = 5 * 1024 * 1024;

const app = new Koa();

app.use(koaBody({ multipart: true, formidable: { maxFileSize, uploadDir } }));
app.use(compress({ threshold: 1024 }));
app.use(api.whitelist.routes());
app.use(staticCache(path.join(__dirname, '../../web/build'), { gzip: true }));
app.use(static(path.join(__dirname, '../../web/build'), { gzip: true }));
app.use(static(path.join(__dirname, '../../files'), { gzip: true }));
app.use(redirectProject);
app.use(redirectHtml);
app.listen(args.WEB_PORT || '3100');

log(`http://localhost:${args.WEB_PORT || 3100}`);
log('---------------------------------------------------');